package org.acghub.mtdb.util;

import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Set;

public final class XSet<E> extends AbstractSet<E> implements Set<E> {
    private final transient java.util.HashMap<E, E> map;

    public XSet() {
        map = new java.util.HashMap<>();
    }

    public XSet(int initialCapacity) {
        map = new java.util.HashMap<>(initialCapacity);
    }

    public XSet(int initialCapacity, float loadFactor) {
        map = new java.util.HashMap<>(initialCapacity, loadFactor);
    }

    @Override
    public Iterator<E> iterator() {
        return map.keySet().iterator();
    }

    @Override
    public int size() {
        return map.size();
    }

    @Override
    public boolean isEmpty() {
        return map.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        return map.containsKey(o);
    }

    @Override
    public boolean add(E e) {
        if (null == e) throw new NullPointerException();
        if (map.containsKey(e)) return false;
        map.put(e, e);
        return true;
    }

    public E xremove(Object o) {
        return map.remove(o);
    }

    @Override
    public boolean remove(Object o) {
        return null != xremove(o);
    }

    @Override
    public void clear() {
        map.clear();
    }

}
